package com.cloudm.technician.ui.modules.cloudm_box.transfer

import android.content.Intent
import android.os.Bundle
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.data.model.SearchInfo
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BaseAdapter
import com.cloudm.technician.ui.base.BaseListFragment
import com.cloudm.technician.ui.modules.cloudm_box.bind_success.BindSuccessActivity
import kotlinx.android.synthetic.main.common_fragment_list.*

/**
 * 作者: wesley
 * 时间: 2017/12/24
 * 描述: ui
 */
class TransferFragment : BaseListFragment<TransferPresenter, SearchInfo>(), TransferContract.View,
        TransferAdapter.OnTransferClickListener, TransferDialogFragment.OnDialogItemClickListener {

    private var info: DeviceInfo? = null
    private var channelId: String? = null
    private var orderNumber: String? = null
    private var url: String? = null

    override val enabledLoadMore: Boolean
        get() = false

    override val enabledRefresh: Boolean
        get() = false

    override val progressEnabled: Boolean
        get() = false

    override val dividerEnabled: Boolean
        get() = false

    private val adapter: TransferAdapter by lazy {
        TransferAdapter()
    }

    companion object {
        private const val TAG = "TransferFragment"
        fun newInstance(info: DeviceInfo?, channelId: String?, orderNumber: String?,
                        url: String?) = TransferFragment().apply {
            arguments = Bundle().apply {
                putParcelable("info", info)
                putString("channelId", channelId)
                putString("orderNumber", orderNumber)
                putString("url", url)
            }
        }
    }

    override fun onItemClick(t: SearchInfo) {

    }

    override fun createAdapter(): BaseAdapter<SearchInfo> {
        adapter.listener = this
        return adapter
    }


    override fun loadData() {
    }

    override fun initInject() {
        DaggerTransferComponent.builder().appComponent(appComponent).transferPresenterModule(
                TransferPresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        info = bundle.getParcelable("info")
        channelId = bundle.getString("channelId", null)
        orderNumber = bundle.getString("orderNumber", null)
        url = bundle.getString("url", null)
    }

    override fun showSearchValueEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.please_input_phone_number_owner))
    }

    override fun showLoadingView() {
        showProgressView()
    }

    fun search(value: String?) {
        presenter?.search(value)
    }

    override fun initEmptyResource() {
        super.initEmptyResource()
        viewStub.layoutResource = R.layout.empty_machine_view
    }

    override fun onTransferClick(searchInfo: SearchInfo) {
        val fragment = TransferDialogFragment.newInstance(info, searchInfo)
        fragment.listener = this
        fragment.show(childFragmentManager, TAG)
    }

    override fun onTransferClick(deviceId: Int, memberId: Int) {
        presenter?.transfer(deviceId, memberId, channelId, orderNumber, url)
    }

    override fun showTransferDialog() {
        showLoadingDialog(mContext.resources.getString(R.string.transfering))
    }

    override fun hideTransferDialog() {
        hideLoadingDialog()
    }

    override fun transferSuccess() {
        val intent = Intent(mContext, BindSuccessActivity::class.java).apply {
            putExtras(Bundle().apply {
                putParcelable("info", info)
            })
        }
        mContext.openActivity(intent)
    }

    override fun transferFailure(errorMsg: String) {
        mContext.toast(errorMsg)
    }
}